<template>
  <section class="audio-resource-item__wrapper" v-if="!selfHide">
    <el-tag class="p-tag__item" size="mini">{{ index +1 }}p</el-tag>
    <el-image :class="{'resource-cover__item':resourceCover}" :src="resourceCover" fit="cover">
      <i class="default-resource-cover__item" slot="error" />
    </el-image>
    <div class="resource-info__group">
      <span class="resource-name__item">{{ resourceName }}</span>
      <el-progress :percentage="percentage" class="upload-progress__item" />
      <div class="upload-info__group">
        <i :class="tipIcon" style="color:#aaa" />
        <span class="resource-size__item" v-if="resourceSize">{{uploadedSize | numberFormatter(2)}} / {{ resourceSize | numberFormatter(2) }}</span>
      </div>
    </div>
    <i @click="removeSelf" class="delete-icon__btn el-icon-delete" />
    <i @click="uploadFail" class="re-upload__btn el-icon-refresh-right" v-if="isError">
      <span>上传出错,点击重试</span>
    </i>
    <el-popover placement="bottom-end" v-model="editPanelShow" width="400">
      <resourceInfoEditPanel :resource="resource" @cancel="editPanelShow=false" @commit="submit" />
      <i class="open-edit-panel__btn el-icon-edit" slot="reference">编辑信息</i>
    </el-popover>
  </section>
</template>

<script>
// api
import { deleteAudioReourceAPI, updateAudioResourceAPI } from "@/api/utils";
// components
import resourceInfoEditPanel from "./resourceInfoEditPanel.vue";

export default {
    name: "AudioResourceItem",
    components: {
        resourceInfoEditPanel,
    },
    props: {
        index: {
            type: Number,
            required: true,
        },
        resource: {
            type: Object,
            required: true,
        },
    },
    data() {
        return {
            selfShow: true,
            editPanelShow: false,
        };
    },
    computed: {
        resourceCover() {
            return this.resource.cover || "";
        },
        resourceName() {
            return this.resource.name || "";
        },
        percentage() {
            // eslint-disable-next-line no-void
            return this.resource.progress === void 0 ? 100 : this.resource.progress;
        },
        resourceSize() {
            return this.resource.size || 0;
        },
        isError() {
            return this.resource.isError;
        },
        uploadedSize() {
            return this.resource.size * (this.percentage / 100);
        },
        tipIcon() {
            const icons = [
                { "el-icon-success": this.resource.tip === undefined },
                { "el-icon-time": this.resource.tip === 0 },
                { "el-icon-loading": this.resource.tip === 1 },
                { "el-icon-upload": this.resource.tip === 2 },
                { "el-icon-success": this.resource.tip === 3 },
            ];
            return icons;
        },
        selfHide() {
            return this.resource.selfHide || false; // 初始是undefined或false
        },
    },
    methods: {
        uploadFail() {
            console.error("我敢肯定,这个上传一定不会出现失败! 2020年5月27日 10:08:13");
            this.$emit("fail", this.resource);
        },
        async removeSelf() {
            const { rid, client } = this.resource;
            this.resource.selfHide = true;
            if (client) client.cancel(); // 有client就代表它是正在上传的任务, 暂停这个上传任务
            if (rid) await deleteAudioReourceAPI(rid); // 有rid 代表它是已经上传成功的, 就可以调接口
        },
        async submit() {
            this.editPanelShow = false;
            const { rid } = this.resource;
            if (rid) await updateAudioResourceAPI(rid, this.resource);
        },
    },
};
</script>

<style lang="scss">
.audio-resource-item__wrapper {
  margin: 20px 0;
  padding: 10px 0 8px 10px;
  display: flex;
  align-items: center;
  position: relative;
  border-radius: 0 5px 5px 0;
  border: 1px solid $tagBorderColor;
  cursor: move;
  cursor: -webkit-grabbing;
  .p-tag__item {
    position: absolute;
    top: -1px;
    right: 100%;
  }
  .resource-cover__item {
    width: 50px;
    height: 50px;
    border-radius: 4px;
    box-shadow: 0 0 1px #000;
    .default-resource-cover__item {
      width: 50px;
      height: 50px;
      line-height: 44px;
      display: block;
      font-size: 14px;
      background: url()
        0px no-repeat;
      color: #222;
    }
  }
  .resource-info__group {
    width: 100%;
    margin-left: 10px;
    .resource-name__item {
      width: 90%;
      height: 16px;
      display: inline-block;
      font-size: 14px;
    }
    .upload-progress__item {
      display: flex;
      align-items: center;
      padding-right: 10px;
    }
  }
  .upload-info__group {
    height: 18px;
    line-height: 18px;
    .resource-size__item {
      font-size: 12px;
    }
  }
  .delete-icon__btn {
    position: absolute;
    top: 10px;
    right: 30px;
    color: $bilipink;
    cursor: pointer;
  }
  .open-edit-panel__btn {
    position: absolute;
    bottom: 8px;
    right: 30px;
    color: $menuActiveText;
    cursor: pointer;
  }
  .re-upload__btn {
    position: absolute;
    bottom: 10px;
    right: 120px;
    font-size: 12px;
    color: $bilipink;
    cursor: pointer;
  }
}
.el-progress__text {
    width: 50px;
}
</style>
